from django.contrib import admin
from django.utils.translation import ugettext_lazy as _

from yubikey.decrypt import modhex_decode

from models import Yubikey
from forms import YubikeyForm

class YubikeyAdmin(admin.ModelAdmin):
    model = Yubikey
    form = YubikeyForm
    fields = ('user', 'public_id', 'aes_key', 'remarks', 'is_active',)
    list_display = ('public_id', 'public_id_hex', 'user', 'is_active', 'created_at', 'modified_at', 'secret_id_set')
    list_filter = ('is_active',)
    raw_id_fields = ('user',)
    ordering = ('-created_at',)
    date_hierarchy = 'created_at'
    search_fields = ('public_id', 'user__email', 'user__username')

    def public_id_hex(self, obj):
        return ''.join(modhex_decode(obj.public_id)).encode('hex')
    public_id_hex.short_description = _('Hex Public ID')

    def secret_id_set(self, obj):
        return len(obj.secret_id) > 0
    secret_id_set.boolean = True
    secret_id_set.short_description = _('Secret ID is set') 

admin.site.register(Yubikey, YubikeyAdmin)
